package com.usian.user.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.usian.common.dto.CompeDto;
import com.usian.common.dto.CompetitionDto;
import com.usian.common.vo.ResponseResult;
import com.usian.user.service.CompetitonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/v1/competition")
public class CompetitonController {
    @Autowired
    CompetitonService competitonService;

    @PostMapping("/getCompeResult")
    public ResponseResult getCompeResult(@RequestBody CompeDto compeDto) {
        return competitonService.getCompeResult(compeDto);
    }

    @GetMapping("/getQue")
    public ResponseResult getQue(Integer compeId, Integer queId) {
        return competitonService.getQue(compeId, queId);
    }

    @GetMapping("/selectAnswerById")
    public ResponseResult selectAnswerById(Integer compeId, Integer queId) {
        return competitonService.selectAnswerById(compeId, queId);
    }

    @PostMapping("/getCompetitionList")
    public ResponseResult getCompetitionList(@RequestBody CompetitionDto competitionDto) {
        return competitonService.getCompetitionList(competitionDto);
    }

    @GetMapping("/getCompetitionById")
    public ResponseResult getCompetitionById(Integer compeId) {
        return competitonService.getCompetitionById(compeId);
    }

    @GetMapping("/addMember")
    public ResponseResult addMember(Integer compeId, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("userId");
        return competitonService.addMember(compeId, userId);
    }

    @GetMapping("/getTeam")
    public ResponseResult getTeam() {
        return competitonService.getTeam();
    }

    @GetMapping("/startCompe")
    public ResponseResult startCompe(Integer compeId) {
        return competitonService.startCompe(compeId);
    }

    @GetMapping("/insertHistory")
    public ResponseResult insertHistory(Integer compeId, Integer queId, Integer answerId) {
        return competitonService.insertHistory(compeId, queId, answerId);
    }

    @GetMapping("judgeAnswer")
    public ResponseResult judgeAnswer(@RequestParam("compeId") Integer compeId) {
        return competitonService.judgeAnswer(compeId);
    }
}
